package MAgentAssignment.gui;

import MAgentAssignment.ontology.concepts.Airport;
import MAgentAssignment.ontology.concepts.Flight;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * Created by IntelliJ IDEA.
 * User: prageeth
 * Date: 2/14/12
 * Time: 7:01 PM
 */
public class MonitorGUI extends JFrame implements ActionListener
{
    private InterfaceAgent myAgent;
    //    private FlightIcon flightIcon = new FlightIcon( );
    //    private JPanel mainPanel = new JPanel();
    private FlightWorldTextArea flightWorldTextArea = new FlightWorldTextArea();
    private RunnableWorld runnableWorld = new RunnableWorld();

    public MonitorGUI( InterfaceAgent a )
    {
        super( a.getLocalName() );
        this.myAgent = a;
        try
        {
            jbInit();
            pack();
            setupGUI();
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
        }
    }

    private void jbInit()
    {
        //        flightWorldTextArea.init();
        //        mainPanel.setLayout( new GridLayout( 2, 2 ) );
        //        mainPanel.add( flightIcon );
        //        getContentPane().add( mainPanel, BorderLayout.SOUTH );
        getContentPane().add( flightWorldTextArea, BorderLayout.CENTER );
        setResizable( false );
    }

    private void setupGUI()
    {
        addWindowListener( new WindowAdapter()
        {
            public void windowClosing( WindowEvent e )
            {
                runnableWorld.setContinueWorld( false );
                MonitorGUI.this.myAgent.doDelete();
            }
        } );

        //        this.flightIcon.addActionListener( this);
    }

    public void actionPerformed( ActionEvent ev )
    {
        //        if( flightIcon == ev.getSource() )
        //        {
        //            // Make the agent terminate when the user closes
        //            // the GUI using the button on the upper right corner
        //            try
        //            {
        //                myAgent.informGUIOperation( "Untitled" );
        //            }
        //            catch( Exception e )
        //            {
        //                JOptionPane.showMessageDialog( MonitorGUI.this, "Invalid values. " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
        //            }
        //        }
    }

    public void startPaintThread()
    {
        runnableWorld.start();
        while( true )
        {
            try
            {
                Thread.sleep( 10000 );
            }
            catch( InterruptedException e )
            {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        //        Thread thread = new Thread( runnableWorld );
        //        thread.start();
    }

    public void showGui()
    {
        pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int centerX = (int) screenSize.getWidth() / 2;
        int centerY = (int) screenSize.getHeight() / 2;
        setLocation( centerX - getWidth() / 2, centerY - getHeight() / 2 );
        this.setSize( 800, 800 );
        super.setVisible( true );
    }

    public void tickStep()
    {
        SwingUtilities.invokeLater( new Runnable()
        {
            public void run()
            {
                flightWorldTextArea.repaint();
                flightWorldTextArea.tickStep();
                flightWorldTextArea.repaint();
            }
        } );

    }

    class RunnableWorld extends javax.swing.SwingWorker
    {
        boolean continueWorld = true;

        @Override protected Object doInBackground() throws Exception
        {
            while( continueWorld )
            {
                flightWorldTextArea.repaint();
                flightWorldTextArea.tickStep();
                flightWorldTextArea.repaint();
                try
                {
                    Thread.sleep( 1000 );
                }
                catch( InterruptedException e )
                {
                    e.printStackTrace();
                }
            }
            return null;
        }

        public void start()
        {
            super.execute();
        }

        public boolean isContinueWorld()
        {
            return continueWorld;
        }

        public void setContinueWorld( boolean continueWorld )
        {
            this.continueWorld = continueWorld;
        }
    }

    public static void main( String[] args )
    {
        MonitorGUI monitorGUI = new MonitorGUI( new InterfaceAgent() );
        monitorGUI.showGui();
        monitorGUI.startPaintThread();
    }

    public void airpotClosed( final Airport airport )
    {

         SwingUtilities.invokeLater( new Runnable()
        {
            public void run()
            {
                 flightWorldTextArea.removeAirports( airport );
            }
        } );
    }

    public void airpotStarted( final Airport airport )
    {
        SwingUtilities.invokeLater( new Runnable()
        {
            public void run()
            {
                 flightWorldTextArea.addAirport( airport );
            }
        } );


    }

    public void flightClosed( Flight flight )
    {
        flightWorldTextArea.removeFlight( flight );
    }

    public void flightStarted( Flight flight, FlightTravelListener flightTravelListener )
    {
        flightWorldTextArea.addFlight( flight, flightTravelListener );
    }
}
